/**
 * @param { string } path
 * @returns { Boolean }
 */
export const isExternal = (path: string) => {
	return /^(https?:|mailto:|tel:)/.test(path)
}

// 获取屏幕缩放比例
export function getRatio() {
	let ratio = 0
	let screen = window.screen
	let ua = navigator.userAgent.toLocaleLowerCase()

	if (window.devicePixelRatio !== undefined) {
		ratio = window.devicePixelRatio
	} else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
		ratio = window.outerWidth / window.innerWidth
	}

	if (ratio) {
		ratio = Math.round(ratio * 100)
	}

	return ratio
}

// 获取当前日期时间
export function getCurrentDate() {
	let now = new Date()
	let year = now.getFullYear()
	let month: any = now.getMonth() + 1
	let day: any = now.getDate()
	let hour: any = now.getHours()
	let minute: any = now.getMinutes()
	let second: any = now.getSeconds()
	let timestamp: string = ''

	month = month > 9 ? month : '0' + month
	day = day > 9 ? day : '0' + day
	hour = hour > 9 ? hour : '0' + hour
	minute = minute > 9 ? minute : '0' + minute
	second = second > 9 ? second : '0' + second
	timestamp = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second

	return timestamp
}

// 获取每个月的所有天数
export function getDays() {
	let now = new Date()
	let year = now.getFullYear()
	let month: any = now.getMonth() + 1
	let day = now.getDate()
	let max = new Date(year, month, 0).getDate() // 获取当月有多少天
	let dateArr = []
	let dateStr = ''

	month = month > 9 ? month : '0' + month

	for (let i = 1; i <= max; i++) {
		dateStr = year + '-' + month + '-' + (i > 9 ? i : '0' + i)
		dateArr.push(dateStr)
	}

	return dateArr
}

// 检测是否登录，false 未登录，true 登录
export const checkLogin = (): boolean => {
	let userInfo = localStorage.getItem('userInfo')

	if (!userInfo || JSON.stringify(userInfo) === '{}') {
		return false
	}

	return true
}

// 获取 localStorage 中的某个值
// available：存储在 localStorage 中的key，filed（可选）：要获取的字段
export const getStorageVal = (available: string, filed?: string): any => {
	let avai = localStorage.getItem(available)
	let val = ''

	if (!avai || avai === '{}') {
		return false
	}

	if (filed) {
		val = JSON.parse(avai)[filed]
	} else {
		val = JSON.parse(avai)
	}

	return val
}

// 删除 localStorage 中得某个值
// key：存储在 localStorage 中的key，filed（可选）：要获取的字段
export const removeStorageVal = (key: string): any => {
	localStorage.removeItem(key)
}
